package com.codeying.utils.component;

/**
 * 分页信息，通用分页组件
 */
public class PagerVO<T> {

    //页码
    private int pageIndex = 1;
    //每页大小
    private int pageSize = 20;
    //记录总数
    private int itemCount;
    //分页链接格式
    private String pagingFormat = "javascript:gotoPage(%s)";

    public PagerVO() {
    }

    public PagerVO(Integer pageIndex, Integer pageSize) {
        this.pageIndex = pageIndex == null ? 1 : pageIndex;
        this.pageSize = pageSize == null ? 20 : pageSize;
    }

    public String getPagingFormat() {
        return pagingFormat;
    }

    public void setPagingFormat(String pagingFormat) {
        this.pagingFormat = pagingFormat;
    }

    public int getItemCount() {
        return itemCount;
    }

    public void setItemCount(int itemCount) {
        this.itemCount = itemCount;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public int getPageIndex() {
        return pageIndex;
    }

    public void setPageIndex(int pageIndex) {
        this.pageIndex = pageIndex;
    }

    //分页内容
    private int pageCount;
    private boolean isFirst;
    private boolean isLast;
    private int pageStart;
    private int pageEnd;
    private boolean hasLessStart;
    private boolean hasGreaterEnd;

    public int getPageCount() {
        return pageCount;
    }

    public boolean getIsFirst() {
        return isFirst;
    }

    public boolean getIsLast() {
        return isLast;
    }

    public int getPageStart() {
        return pageStart;
    }

    public int getPageEnd() {
        return pageEnd;
    }

    public boolean getHasLessStart() {
        return hasLessStart;
    }

    public boolean getHasGreaterEnd() {
        return hasGreaterEnd;
    }

    public void init() {

        //计算页数
        pageCount = (int) Math.ceil(itemCount * 1d / pageSize);

        //第一页或者是最后一页
        isFirst = pageIndex <= 1;
        isLast = pageIndex >= pageCount;

        //确定开始页码
        pageStart = pageIndex / 10 * 10;
        if (pageIndex % 10 == 0) {
            pageStart -= 10;
        }
        pageStart = pageStart + 1;
        //确定结束页码
        pageEnd = pageStart + 9;
        if (pageEnd > pageCount) {
            pageEnd = pageCount;
        }
        if (pageCount == 0) {
            pageEnd = 1;
        }

        hasLessStart = pageStart > 1;
        hasGreaterEnd = (pageEnd + 1) < pageCount;
    }
}
